home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / qbsnip.zip / BRESNHAM.BAS next >
BASIC Source File  |  1997-06-20  |  2KB  |  61 lines

  1. '_|_|_|   BRESNHAM.BAS
  2. '_|_|_|   This program demonstrates the Bresenham Algorithms
  3. '_|_|_|   for the drawing of lines and circles, using PSET.
  4. '_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
  5. '_|_|_|   No warrantee or guarantee is implied or given.
  6. '_|_|_|   Released to   PUBLIC DOMAIN   by Kurt Kuzba. (4/16/96)
  7.  
  8. DECLARE SUB BLine (x%, y%, x2%, y2%, c%)
  9. DECLARE SUB BCircle (x%, y%, r%, c%)
  10.  
  11. SCREEN 13
  12.  
  13. CONST HIGH = 200   'The Bresenham Cirlce needs to know the screen dimensions
  14. CONST WIDE = 320
  15.  
  16. BCircle 159, 99, 65, 77
  17. BLine 0, 0, WIDE, HIGH, 14
  18. pause$ = INPUT$(1)
  19.  
  20. SCREEN 0, 0, 0, 0: WIDTH 80: COLOR 7, 0: CLS : END
  21.  
  22. SUB BCircle (xc%, yc%, r%, c%)
  23. '_|_|_|   Bresenham Circle Drawing Algorithm
  24. '_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
  25.    x% = 0: d% = 2 * (1 - r%): W% = 2 * WIDE \ HIGH
  26.    WHILE r% >= 0
  27.       PSET (xc% + x%, yc% + r%), c%
  28.       PSET (xc% + x%, yc% - r%), c%
  29.       PSET (xc% - x%, yc% + r%), c%
  30.       PSET (xc% - x%, yc% - r%), c%
  31.       IF (d% + r%) > 0 THEN r% = r% - 1: d% = d% - W% * r% - 1
  32.       IF x% > d% THEN x% = x% + 1: d% = d% + 2 * x% + 1
  33.    WEND
  34. END SUB
  35.  
  36. SUB BLine (x%, y%, x2%, y2%, c%)
  37. '_|_|_|   Bresenham Line Drawing Algorithm
  38. '_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
  39.    i% = 0: steep% = 0: e% = 0
  40.    IF (x2% - x%) > 0 THEN sx% = 1:  ELSE sx% = -1
  41.    dx% = ABS(x2% - x%)
  42.    IF (y2% - y%) > 0 THEN sy% = 1:  ELSE sy% = -1
  43.    dy% = ABS(y2% - y%)
  44.    IF (dy% > dx%) THEN
  45.       steep% = 1
  46.       SWAP x%, y%
  47.       SWAP dx%, dy%
  48.       SWAP sx%, sy%
  49.    END IF
  50.    e% = 2 * dy% - dx%
  51.    FOR i% = 0 TO dx% - 1
  52.       IF steep% = 1 THEN PSET (y%, x%), c%:  ELSE PSET (x%, y%), c%
  53.       WHILE e% >= 0
  54.          y% = y% + sy%: e% = e% - 2 * dx%
  55.       WEND
  56.       x% = x% + sx%: e% = e% + 2 * dy%
  57.    NEXT
  58.    PSET (x2%, y2%), c%
  59. END SUB
  60.  
  61.